let input = "2 3 4 5 1 2 1";
let arr = input.split(" ").map((val) => parseInt(val));
while (arr.length) {
  let flag = 1;
  let t = arr[0];
  let res = [t];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] < t) {
      if (flag == 1) {
        arr.splice(0, 1);
        i--;
      }
      flag = 0;
      res.push(arr[i]);
      arr.splice(i, 1);
      i--;
    }
  }
  if (flag == 1) break;
  console.log(res.join(" "));
}
console.log(Number.MIN_VALUE)